package com.polaris.live.ui.live.activity.end.live

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.polaris.live.adapter.GainCallEndAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.DateUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.ActivityLiveEndBinding
import com.polaris.live.dialog.NormalNoticeDialog
import com.polaris.live.resp.back_resp.LiveStopReceiptResp
import com.polaris.live.resp.bean.CallEndBeansBean
import com.polaris.live.resp.event.LiveBannedEvent
import com.polaris.live.translation.R
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.image.ImageUtils
import jp.wasabeef.glide.transformations.BlurTransformation
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 主播关闭直播结算页面
 *
 * @author Created by 半仙 on 2023/11/4/004 10:12
 */
class LiveEndActivity : BaseVbActivity<BaseViewModel, ActivityLiveEndBinding>() {

    private val mAdapter by lazy { GainCallEndAdapter() }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .titleBar(mBinding.icBack)
            .fullScreen(false)
            .transparentNavigationBar()
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        val endInfo = intent.getParcelableExtra<LiveStopReceiptResp>(LiveConst.Params.LIVE_END_INFO)
        val type = intent.getIntOrString(LiveConst.Params.LIVE_TYPE, -1)

        mBinding.apply {
            val user = UserManager.getUser()

            userAvatar.setImageUrlMiddle(user.avatar, user.userId)
            avFv.setFrameString(user.decoration?.avatarFrame)
            Glide.with(this@LiveEndActivity)
                .load(ImageUtils.imgPath(user.avatar))
                .transform(BlurTransformation(15, 3))
                .into(backBigImg)

            country.setCountry(user.profile?.secondCountry)
            nickName.text = user.nickname
            vipView.setVipLevel(user.profile?.vipLevel)
            val richs = user.account?.richs
            val charms = user.account?.charms
            if (richs != null && charms != null) {
                if (CommonUtils.isMan(user.sex)) {
                    otherLevel.setRichsLevel(richs)
                } else {
                    otherLevel.setCharmsLevel(charms)
                }
            }
            val list = mutableListOf<CallEndBeansBean>()
            if (endInfo != null) {
                val time = endInfo.duration.let { DateUtils.formatDuration(it) }
                    .convertArabicDigitsToNormal()
                //如果是仅坐等
                if (type == LiveConst.Type.ONLY_MATCH) {
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.video_end), time, false
                        )
                    )
                } else {
                    list.add(
                        CallEndBeansBean(
                            "${StringUtils.getString(R.string.living_time)}:", time, false
                        )
                    )
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.people_num),
                            endInfo.joinUser.toString(),
                            false
                        )
                    )
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.live_all_num),
                            "${endInfo.gainCoins + (endInfo.gameGainCoins ?: 0)}",
                            true
                        )
                    )
                }
                //如果是仅坐等或者直播坐等  要把视频收益加进去
                if (type == LiveConst.Type.ONLY_MATCH || type == LiveConst.Type.LIVE_AND_MATCH) {
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.call_all_num),
                            endInfo.callGainCoins.toString(),
                            true
                        )
                    )
                }
            }
            gainRecycler.apply {
                adapter = mAdapter
                layoutManager = GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
            }
            mAdapter.setList(list)
        }

        initListener()
    }

    private fun initListener() {
        mBinding.icBack.setOnSingleClickListener {
            finish()
        }
        mBinding.confirm.setOnSingleClickListener {
            finish()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onLiveBanned(event: LiveBannedEvent) {
        EventBus.getDefault().removeStickyEvent(event)

        event.message?.let {
            XPopup.Builder(this)
                .apply {
                    if (it.shadow != null) {
                        if (it.shadow.enabled) {
                            hasShadowBg(true)
                        } else {
                            hasShadowBg(false)
                        }
                        if (!it.shadow.clickClose) {
                            dismissOnTouchOutside(false)
                        } else {
                            dismissOnTouchOutside(true)
                        }
                    } else {
                        hasShadowBg(true)
                    }
                }
                .isDestroyOnDismiss(true)
                .asCustom(NormalNoticeDialog(this, it))
                .show()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}